package com.netdisk.controller;

import com.netdisk.dto.StorageStatsDTO;
import com.netdisk.dto.UserActivityDTO;
import com.netdisk.dto.request.user.InfoDTO;
import com.netdisk.dto.request.user.LoginDTO;
import com.netdisk.dto.request.user.RegisterDTO;
import com.netdisk.dto.response.user.DetailedInfoDTO;
import com.netdisk.dto.response.user.StorageInfoDTO;
import com.netdisk.dto.response.user.UserDTO;
import com.netdisk.mapper.UserMapper;
import com.netdisk.service.UserActivityService;
import com.netdisk.service.UserService;
import com.netdisk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserActivityService userActivityService;

    /**
     * 用户登录
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result<UserDTO> login(@RequestBody LoginDTO loginDTO){
        UserDTO userDTO = userService.login(loginDTO);
        return Result.success(userDTO);
    }

    /**
     * 用户注册
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Result<UserDTO> register(@RequestBody RegisterDTO registerDTO){
        UserDTO userDTO = userService.register(registerDTO);
        return Result.success(userDTO);
    }

    public static void main(String[] args) {
        System.out.println("Test");
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/info")
    public Result<DetailedInfoDTO> info(@RequestParam Integer id) {
        InfoDTO infoDTO = new InfoDTO();
        infoDTO.setId(id);
        DetailedInfoDTO detailedInfoDTO = userService.getDetailedInfo(infoDTO);
        return Result.success(detailedInfoDTO);
    }

    @ApiOperation(value = "获取存储统计信息")
    @GetMapping("/storage/stats")
    public Result<StorageInfoDTO> storageStats(@RequestParam Integer id){
        InfoDTO infoDTO = new InfoDTO();
        Integer fileCount = userMapper.countFiles(id);
        infoDTO.setId(id);
        StorageInfoDTO storageInfoDTO = userService.getStorageInfo(infoDTO);
        storageInfoDTO.setFileCount(fileCount);
        return Result.success(storageInfoDTO);
    }

    @ApiOperation(value = "获取用户最近活动")
    @GetMapping("/activities")
    public Result<List<UserActivityDTO>> getActivityList(@RequestParam Integer id) {
        List<UserActivityDTO> activityList = userActivityService.getActivityListById(id);
        return Result.success(activityList);
    }

}
